package website.seamonster.ethegg.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.seamonster.ethegg.dataobject.ChickenFarmView;
import website.seamonster.ethegg.dto.ChickenDTO;
import website.seamonster.ethegg.dto.EggDTO;
import website.seamonster.ethegg.repository.ChickenFarmViewRepository;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 罗海量
 * 2018-07-23 00:51
 */
@lombok.extern.slf4j.Slf4j
@Service
public class ChickenFarmService {
    @Autowired
    private ChickenFarmViewRepository repository;
    @Autowired
    private EggService eggService;

    /**
     * 获取特定用户养鸡场列表（有哪些鸡，每只鸡有哪些蛋）
     * @param userId
     * @return
     */
    public List<ChickenDTO> getChickenFarmList(String userId){
//        List<ChickenFarmView> farmList = repository.findByUserIdOrderByChickenId(userId);

        //这TM是Spring Boot JPA的bug吧
        List<ChickenFarmView> farmList = new ArrayList<>();
        List tmpList = repository.findChickensByUserId(userId);
        for (Object o:tmpList){
            Object[] os = (Object[])o;
            if (os[1]==null){
                continue;
            }
            ChickenFarmView c = new ChickenFarmView();
            c.setUserId((String) os[0]);
            c.setChickenId(((BigInteger) os[1]).longValue());
            c.setBornNum((Integer) os[3]);
            c.setIsAlive(((Byte) os[4]).intValue()==1);
            c.setEggId(((BigInteger) os[5]).longValue());
            c.setEggStatus(((Byte) os[6]).intValue());
            farmList.add(c);
        }
        Set<Long> chickenIdSet = new LinkedHashSet<Long>(farmList.size());
//        farmList.stream().map(e -> chickenIdSet.add(e.getChickenId()));
        farmList.stream().forEach(e -> chickenIdSet.add(e.getChickenId()));

        //chickenIdList 按顺序排列的chicken id列表
        List<Long> chickenIdList = new ArrayList<Long>(chickenIdSet.size());
        chickenIdList.addAll(chickenIdSet);

        List<ChickenDTO> chickenDTOList = new ArrayList<ChickenDTO>(chickenIdList.size());

        chickenIdList.forEach(e -> {
            ChickenDTO chickenDTO = new ChickenDTO();
            chickenDTOList.add(chickenDTO);
        });

        farmList.stream().forEach( e-> {
            Long chickenId = e.getChickenId();
            if (chickenId != null) {
                ChickenDTO chickenDTO = chickenDTOList.get(chickenIdList.indexOf(chickenId));

                if (chickenDTO.getId() == null) {
                    chickenDTO.setId(chickenId);
                    chickenDTO.setChickenName(e.getChickenName());
                    chickenDTO.setBornNum(e.getBornNum());
                    chickenDTO.setAlive(e.getIsAlive());
                }

                if (e.getEggId() != null) {
                    if (chickenDTO.getEggList() == null) {
                        chickenDTO.setEggList(new ArrayList<EggDTO>());
                    }
                    EggDTO eggDTO = new EggDTO();
                    eggDTO.setId(e.getEggId());
                    eggDTO.setEggStatus(e.getEggStatus());
                    chickenDTO.getEggList().add(eggDTO);
                }
            }

        });

        //查询该用户正在孵化中的蛋，把它当作鸡（未出生的鸡）一并返回
        int broodEggsCnt = eggService.getBroodEggsCountByUserId(userId);
        for (int i=0 ; i<broodEggsCnt; i++){
            ChickenDTO chickenDTO = new ChickenDTO();
            chickenDTO.setEgg(true);
            chickenDTOList.add(chickenDTO);
        }

        return chickenDTOList;

    }
}
